package com.dog.basics.service.common.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.common.BannerGenusAddDto;
import com.dog.basics.bean.dto.admin.common.BannerGenusEditDto;
import com.dog.basics.bean.model.common.SysBanner;
import com.dog.basics.bean.model.common.SysBannerGenus;
import com.dog.basics.dao.common.SysBannerGenusMapper;
import com.dog.basics.dao.common.SysBannerMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.ISysBannerGenusService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 广告所属地方 服务实现类
 * </p>
 *
 * @author fengjukang
 * @since 2019-12-13
 */
@Service
@AllArgsConstructor
public class SysBannerGenusServiceImpl extends ServiceImpl<SysBannerGenusMapper, SysBannerGenus> implements ISysBannerGenusService {
    private final SysBannerMapper bannerMapper;

    /**
     * @Description 增加广告所属地方
     * @Author zhangmr
     * @Date 2020/8/6 0006 上午 9:19
     * @Param
     * @Return
     * @Exception
     */
    @Override
    public Boolean saveObj(BannerGenusAddDto obj) {
        SysBannerGenus sysBannerGenus = BeanUtil.toBean(obj, SysBannerGenus.class);
        sysBannerGenus.setCreateBy(SecurityUtils.getSysUser().getUserId());
        sysBannerGenus.setCreateTime(DateUtil.currentSeconds());
        return this.save(sysBannerGenus);
    }

    /**
     * @Description 删除广告所属地方
     * @Author zhangmr
     * @Date 2020/8/6 0006 上午 9:24
     * @Param
     * @Return
     * @Exception
     */
    @Override
    public Boolean deleteObj(Long id) {
        SysBannerGenus bannerGenus = this.getById(id);
        Validator.validateNotNull(bannerGenus, "数据不存在！");
        List<SysBanner> sysBanners = bannerMapper.selectList(Wrappers.<SysBanner>lambdaQuery()
                .eq(SysBanner::getGenusId, id)
        );
        Validator.validateTrue(CollUtil.isEmpty(sysBanners), "有关联数据无法删除！");
        return this.removeById(bannerGenus);
    }

    /**
     * @Description 修改广告所属地方
     * @Author zhangmr
     * @Date 2020/8/6 0006 上午 9:30
     * @Param
     * @Return
     * @Exception
     */
    @Override
    public Boolean editObj(BannerGenusEditDto obj) {
        SysBannerGenus bannerGenus = this.getById(obj.getId());
        Validator.validateNotNull(bannerGenus, "数据不存在！");
        BeanUtil.copyProperties(obj, bannerGenus);
        bannerGenus.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        bannerGenus.setUpdateTime(DateUtil.currentSeconds());
        return this.updateById(bannerGenus);
    }
}
